//
//  DTTextPicker.m
//  Dentist
//
//  Created by Sckjsckj on 3/24/15.
//  Copyright (c) 2015 Sckjsckj. All rights reserved.
//

#import "XMCommonPicker.h"
#import "UIColor+RGB.h"

#import "XMScreenAdaptation.h"
//#import "XMSDKManager.h"
//#import "ESignBuddle.h"
#define KNAV_HEIGHT 40.0f * [XMScreenAdaptation adapterMultipleByWidth]

@interface XMCommonPicker ()<UIPickerViewDataSource, UIPickerViewDelegate>
{
    NSInteger _selectedIndex;
    NSInteger _component;
    NSInteger _selectedSubIndex;
}

@property (nonatomic, strong) UIView *headerView;
@property (nonatomic, strong) UIPickerView *pickerView;
@property (nonatomic, strong) NSString *selectedString;
@property (nonatomic, strong) NSString *selectedSubString;
@property (nonatomic, strong) UIView *m_maskView;
@property (nonatomic, strong) UIView *m_sheetBgView;
@property (nonatomic, strong) UIButton *cancelButton;
@property (nonatomic, strong) UIButton *okButton;

@end

@implementation XMCommonPicker

- (instancetype)initWithFrame:(CGRect)frame component:(NSInteger)component {
    self = [super initWithFrame:frame];
    
    if (self) {
        self.hidden = YES;
        _component = component;
        [self createMaskView];
        [self createHeaderView];
        [self creatPicker];
        
    }
    
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.m_maskView.frame = self.bounds;
    
    CGRect valueFrame = self.m_sheetBgView.frame;
    valueFrame.origin.y = (self.frame.size.height * 2) / 3 - 140;
    valueFrame.size.width = self.frame.size.width;
    valueFrame.size.height = self.frame.size.height / 3 + 140;
    self.m_sheetBgView.frame = valueFrame;
    
    valueFrame = self.headerView.frame;
    valueFrame.size.width = self.frame.size.width;
    self.headerView.frame = valueFrame;
    
    valueFrame = self.okButton.frame;
    valueFrame.origin.x = self.frame.size.width - 10 * [XMScreenAdaptation adapterMultipleByWidth] - valueFrame.size.width;
    self.okButton.frame = valueFrame;
    
    valueFrame = self.pickerView.frame;
    valueFrame.size.width = self.frame.size.width;
    valueFrame.size.height = self.m_sheetBgView.bounds.size.height - self.headerView.frame.size.height;
    self.pickerView.frame = valueFrame;
}


- (void)createHeaderView{
    self.m_sheetBgView = [[UIView alloc] initWithFrame:(CGRect){0, (self.frame.size.height * 2) / 3 - 140, self.frame.size.width, self.frame.size.height / 3 + 140}];
    self.m_sheetBgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:self.m_sheetBgView];
    
    self.headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, KNAV_HEIGHT)];
    [self.headerView setBackgroundColor:[UIColor whiteColor]];
    self.headerView.layer.borderColor = [UIColor colorWithRGB:0xeeeeee].CGColor;
    self.headerView.layer.borderWidth = 0.5;
    [self.m_sheetBgView addSubview:self.headerView];
    
    self.cancelButton = [self setCancelBtnWithTitle:@"取消" textColor:[UIColor colorWithRGB:0x1f78d3] image:nil];
    self.okButton = [self setOkBtnWithTitle:@"完成" textColor:[UIColor colorWithRGB:0x1f78d3] image:nil];
    
    
}

- (UIButton *)setCancelBtnWithTitle:(NSString *)title textColor:(UIColor *)color image:(NSString *)imageName{
    CGFloat width = 50 * [XMScreenAdaptation adapterMultipleByWidth];
    CGFloat padding = 10 * [XMScreenAdaptation adapterMultipleByWidth];
    UIButton *cancelBtn = [self buttonWithTitle:title image:[UIImage imageNamed:imageName] color:color frame:CGRectMake(padding, 0, width, KNAV_HEIGHT)];
    [cancelBtn addTarget:self action:@selector(onCancelBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    return cancelBtn;
    
}

- (UIButton *)setOkBtnWithTitle:(NSString *)title textColor:(UIColor *)color image:(NSString *)imageName{
    CGFloat width = 50 * [XMScreenAdaptation adapterMultipleByWidth];
    CGFloat padding = 10 * [XMScreenAdaptation adapterMultipleByWidth];
    UIButton *okBtn = [self buttonWithTitle:title image:[UIImage imageNamed:imageName] color:color frame:CGRectMake(self.frame.size.width - padding - width, 0, width, KNAV_HEIGHT)];
    [okBtn addTarget:self action:@selector(onOkBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    return okBtn;
}

- (UIButton *)buttonWithTitle:(NSString*)aTitle image:(UIImage*)image color:(UIColor *)color frame:(CGRect)frame
{
    UIFont *font=[UIFont systemFontOfSize:15];
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    if (aTitle == nil) {
        aTitle = @"";
    }
    if (image != nil) {
        [button setImage:image forState:UIControlStateNormal];
        [button setImage:image forState:UIControlStateHighlighted];
    }
    button.frame = frame;
    [button setTitle:aTitle forState:UIControlStateNormal];
    button.titleLabel.font = font;
    [button setTitleColor:color forState:UIControlStateNormal];
    [self.headerView addSubview:button];
    return button;
    
}

- (void)creatPicker{
    self.pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, self.headerView.bounds.size.height, self.frame.size.width, self.m_sheetBgView.bounds.size.height - self.headerView.frame.size.height)];
    self.pickerView.delegate = self;
    self.pickerView.showsSelectionIndicator = YES;
    self.pickerView.backgroundColor = [UIColor whiteColor];
    self.pickerView.dataSource = self;
    [self.m_sheetBgView addSubview:self.pickerView];
}

- (void)createMaskView{
    self.m_maskView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    self.m_maskView.backgroundColor = [UIColor blackColor];
    self.m_maskView.alpha = 0.4;
    self.m_maskView.hidden = YES;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onMaskClicked:)];
    [self.m_maskView addGestureRecognizer:tap];
    
    [self addSubview:self.m_maskView];
}

- (void)reloadTextPicker {
    [self.pickerView reloadAllComponents];
}


#pragma mark - action methods

- (void)onMaskClicked:(UITapGestureRecognizer *)tap{
    [self hide];
}

- (void)onCancelBtnClicked:(id)sender{
    [self hide];
}

- (void)onOkBtnClicked:(id)sender{
    if (_commonPickerDelegate && [_commonPickerDelegate respondsToSelector:@selector(picker:selectedRow:selectedSubRow:)]) {
        [_commonPickerDelegate picker:self selectedRow:_selectedIndex selectedSubRow:_selectedSubIndex];
    }
    [self hide];
}

- (void)show:(UIScrollView *)scrolld{
    if (!self) {
        
        return;
    }
    [UIView animateWithDuration:0.1
                          delay:0.1
                        options: UIViewAnimationOptionCurveEaseIn
                     animations:^{
                         
                         [scrolld addSubview:self];
                         self.hidden = NO;
                         CGRect frame = self.m_sheetBgView.frame;
                         frame.origin.y = self.frame.size.height - self.m_sheetBgView.frame.size.height;
                         self.m_sheetBgView.frame = frame;
                         self.m_maskView.hidden = NO;
                     }
                     completion:^(BOOL finished){

                         
                     }];
}

- (void)hide{
    if (!self) {
        return;
    }
    [UIView animateWithDuration:0.1
                          delay:0.1
                        options: UIViewAnimationOptionCurveEaseOut
                     animations:^{
                         CGRect frame = self.m_sheetBgView.frame;
                         frame.origin.y = self.frame.size.height;
                         self.m_sheetBgView.frame = frame;
                         self.hidden = YES;
                         self.m_maskView.hidden = YES;
                         
                     }
                     completion:^(BOOL finished){
                         [self removeFromSuperview];
                     }];
}

#pragma mark - UIPickerView Delegate
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    return _component;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    
    if (component == 0) {
        return self.dataArr.count;
    }
    
    return self.subDataArr.count;
}

- (NSString *)pickerView:(UIPickerView *)pickerView
             titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    if (component == 0) {
        return [self.dataArr objectAtIndex:row];
    }
    return [self.subDataArr objectAtIndex:row];
}

//监听轮子的移动
- (void)pickerView:(UIPickerView *)pickerView
      didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    if (component == 0) {
        _selectedIndex = row;
        self.selectedString = [self.dataArr objectAtIndex:row];

    }
    if (component == 1){
        _selectedSubIndex = row;
        self.selectedSubString = [self.subDataArr objectAtIndex:row];
    }
    
}

- (void)setCurrentIndex:(NSInteger)index subIndex:(NSInteger)subIndex{
    if (index < self.dataArr.count) {
        [self.pickerView selectRow:index inComponent:0 animated:YES];
        self.selectedString = self.dataArr[index];
        _selectedIndex = index;
    }
    if (_component > 1 && subIndex < self.subDataArr.count) {
        [self.pickerView selectRow:subIndex inComponent:1 animated:YES];
        self.selectedString = self.subDataArr[subIndex];
        _selectedSubIndex = subIndex;
    }
}


@end
